import 'package:dio/dio.dart';
import 'package:mall/core/services/auth.dart';
import 'package:mall/core/providers/view_model.dart';

class UserQrModel extends ViewModel {
  final Auth auth;
  String _errorMessage;
  String _url;

  UserQrModel(this.auth) : super() {
    _initData();
  }

  String get errorMessage => _errorMessage;

  String get url => _url;

  String get inviteCode => Uri.parse(url).queryParameters['ic'];

  Future<ViewState> _initData() async {
    viewState = ViewState.busy;
    try {
      _url = await auth.userQr();
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
